 /* 
 * File:   cBackgroundScene.cpp
 * Author: josep
 * 
 * Created on 10 / desembre / 2010, 00:57
 */

#include <iostream>

#include "cBackgroundScene.h"
#include "cCamera.h"

cBackgroundScene::cBackgroundScene() {
    nRepeatImageX=-1;
    nRepeatImageY=-1;
    bRepeatX=false;
    bRepeatY = false;
}

cBackgroundScene::cBackgroundScene(const cBackgroundScene& orig):cScene(orig){
    nRepeatImageX = orig.nRepeatImageX;
    nRepeatImageY = orig.nRepeatImageY;
    bRepeatX = orig.bRepeatX;
    bRepeatY = orig.bRepeatY;
}

cBackgroundScene::~cBackgroundScene() {
}

void cBackgroundScene::Init(){
    //cVisible::Init();
    if(bRepeatX){
        SafetyMarginX = pCamera->GetWidth() >> 1;
        int gw = pCamera->GetWidth()+(SafetyMarginX<<1);
        nRepeatImageX = gw/GetTextureWidth();
        if(gw%GetTextureWidth()>0){
            nRepeatImageX++;
        }
        InitialOffsetX-=SafetyMarginX;
        BackgroundWidth=GetTextureWidth()*nRepeatImageX;
    }else{
        SafetyMarginX = 0;
        InitialOffsetX-=0;
        nRepeatImageX=1;
        BackgroundWidth=GetTextureWidth();
    }

    if(bRepeatY){
        SafetyMarginY = pCamera->GetHeight() >> 1;
        int gh = pCamera->GetHeight()+(SafetyMarginY<<1);
        nRepeatImageY = gh/GetTextureHeight();
        if(gh%GetTextureHeight()>0){
            nRepeatImageY++;
        }
        InitialOffsetY-=SafetyMarginY;
        BackgroundHeight=GetTextureHeight()*nRepeatImageY;
    }else{
        SafetyMarginY = 0;
        InitialOffsetY-=0;
        nRepeatImageY=1;
        BackgroundHeight=GetTextureHeight();
    }

}

void cBackgroundScene::Init(cTexture *texture, bool repeatX, bool repeatY,
                                float SpeedReductionFactorX,
                                float SpeedReductionFactorY,
                                float InitialOffsetX,
                                float InitialOffsetY,
                                cCamera *Camera){
    cScene::Init(texture, Camera);
    this->SpeedReductionFactorX=SpeedReductionFactorX;
    this->SpeedReductionFactorY=SpeedReductionFactorY;
    bRepeatX=repeatX;
    bRepeatY=repeatY;
    if(InitialOffsetX!=0){
        this->InitialOffsetX=InitialOffsetX;
    }
    if(InitialOffsetY!=0){
        this->InitialOffsetY=InitialOffsetY;
    }
    if(pCamera!=NULL){
        Init();
    }
}

void cBackgroundScene::Init(cCamera* Camera){
    pCamera=Camera;
    if(pCamera!=NULL){
        Init();
    }
}

void cBackgroundScene::Draw(){
    cTexture*texture = GetTexture();
    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, texture->GetID());
    glBegin(GL_QUADS);

    InitialOffsetX += SpeedReductionFactorX*pCamera->GetSpeedX()*pCamera->GetDirectionX();
    InitialOffsetY += SpeedReductionFactorY*pCamera->GetSpeedY()*pCamera->GetDirectionY();

    /*
    cout << "offestX: " << InitialOffsetX << " offsetY: " << InitialOffsetY << "\n";
    cout << "valor " << SpeedReductionFactorX*pCamera->GetSpeedX()*pCamera->GetDirectionX();
    */

    if(bRepeatX && ((pCamera->GetPosX()-SafetyMarginX)<=InitialOffsetX)){
        InitialOffsetX-=GetTextureWidth();
    }else if(bRepeatX && ((pCamera->GetPosX()+SafetyMarginX+pCamera->GetWidth())
                                >=(InitialOffsetX+BackgroundWidth))){
        InitialOffsetX+=GetTextureWidth();
    }
    if(bRepeatY && ((pCamera->GetPosY()-SafetyMarginY)<=InitialOffsetY)){
        InitialOffsetY-=GetTextureHeight();
    }else if(bRepeatY && ((pCamera->GetPosY()+SafetyMarginY+pCamera->GetHeight())
                                >=(InitialOffsetY+BackgroundHeight))){
        InitialOffsetY+=GetTextureHeight();
    }
    
    for(int i=0; i<nRepeatImageX; i++){
        for(int j=0; j<nRepeatImageY; j++){
            int offsetx = InitialOffsetX + i*GetTextureWidth();
            int offsety = InitialOffsetY + j*GetTextureHeight();
            glTexCoord2f(0.f,0.f);
            glVertex2i(offsetx, GetTextureHeight()+offsety);
            glTexCoord2f(1.f,0.f);
            glVertex2i(GetTextureWidth()+offsetx, GetTextureHeight()+offsety);
            glTexCoord2f(1.f,1.f);
            glVertex2i(GetTextureWidth()+offsetx, offsety);
            glTexCoord2f(0.f,1.f);
            glVertex2i(offsetx, offsety);
        }
    }
    glEnd();
    glDisable(GL_TEXTURE_2D);
}

